﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class ReturnToFactorySend : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            ReturnToFactoryService service = new ReturnToFactoryService();
            ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
            ProviderService providerservice = new ProviderService();
            Provider provider = providerservice.GetProviderByCode(form.ProvideCode);

            if (provider != null)
            {
                providerNameLabel.Text = provider.ProviderName;
                providerCodeLabel.Text = provider.ProviderCode;
                connectPersonLabel.Text = provider.Contact1Name + "," + provider.Contact2Name;
                addressLabel.Text = provider.Address;
                phoneLabel.Text = provider.Contact1Phone + "," + provider.Contact2Phone;
                emailLabel.Text = provider.Contact1Email + "," + provider.Contact2Email;
            }

            factoryRMACodeLabel.Text = form.Code;
            runningNumberLabel.Text = form.RunningNumber;
            rtfDateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
            rmaCodeLabel.Text = form.ApplicationProduct.ApplicationForm.ApplicationCode;
            productTypeLabel.Text = form.ApplicationProduct.ProductType.ProductTypeName;
            productSerialNumberLabel.Text = form.ApplicationProduct.ProductSerialNumber;
            versionLabel.Text = form.ApplicationProduct.ProductVersionNumber;
            problemDescriptionLabel.Text = form.ApplicationProduct.ProductProblemDescription;
            providerLabel.Text = form.ProvideCode;
            requireLabel.Text = form.Requirement;
            tipLabel.Text = form.Tip;
            receiveProductTimeLabel.Text = form.ApplicationProduct.ApplicationForm.ReceiveDateTime.Value.ToString("yyyy-MM-dd");
            applyTimeLabel.Text = form.ApplyDate.Value.ToString("yyyy-MM-dd");
            sendDateTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            bodyTextBox.Text = string.Format(WebConfigurationManager.AppSettings["FCNotice"], provider.Contact1Name, form.ApplicationProduct.ProductType.ProductTypeName, form.ApplicationProduct.ProductSerialNumber);
            toNameLbl.Text = provider.Contact1Name;
            toEmailLbl.Text = provider.Contact1Email;
            if (!form.NoticeCompanyDate.HasValue)
            {
                notifyLinkButton.Enabled = true;                
            }
            else if (!form.SendDate.HasValue)
            {
                notifyLinkButton.Enabled = false;
                saveLinkButton.Enabled = true;
            }
            else
            {
                notifyLinkButton.Enabled = false;
                saveLinkButton.Enabled = true;
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                ReturnToFactoryService service = new ReturnToFactoryService();
                ReturnToFactoryForm form = service.GetReturnToFactoryFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                form.Status = "返厂-待工厂收货确认";
                form.SendDate = Convert.ToDateTime(sendDateTextBox.Text);
                service.Update(form);

                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('发货成功');location.href='ReturnToFactoryListNeedSend.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactorySend.aspx::saveLinkButton_Click");
                throw ex;
            }
        }

        protected void notifyLinkButton_Click(object sender, EventArgs e)
        {            
            notificationModalPopupExtender.Show();
        }

        protected void sendEmailLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                EmailService.SendEmail(toEmailLbl.Text, "返厂发货通知", bodyTextBox.Text);
                ReturnToFactoryService service = new ReturnToFactoryService();
                ReturnToFactoryForm form = service.GetReturnToFactoryFormProductDetail(long.Parse(Request.QueryString[Constants.QUERYSTR_RETURNTOFACTORY_ID]));
                form.NoticeCompanyDate = DateTime.Now;
                service.Update(form);


                Response.Redirect(Request.Url.ToString());
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "ReturnToFactorySend.aspx::sendEmailLinkButton_Click");
                throw ex;
            }
        }
    }
}
